namespace DotNetNuke.Services.Mail
{
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Diagnostics;
    using System.Text;
    using System.Web.UI.WebControls;

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The SendBulkEMail Class is a helper class tht manages the sending of Email
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// [cnurse]	9/13/2004	Updated to reflect design changes for Help, 508 support
    /// and localisation
    /// </history>
    /// -----------------------------------------------------------------------------
    public class SendBulkEmail
    {
        public string Administrator;
        public string Attachment;
        public string Body;
        public MailFormat BodyFormat;
        public string Heading;
        public MailPriority Priority;
        public ArrayList Recipients;
        public string SendMethod;
        public string SMTPAuthentication;
        public string SMTPPassword;
        public string SMTPServer;
        public string SMTPUsername;
        public string Subject;

        public SendBulkEmail()
        {
        }

        public SendBulkEmail(ArrayList recipients, string priority, string format, string portalAlias)
        {
            this.Recipients = recipients;
            switch (priority)
            {
                case "1":
                    this.Priority = MailPriority.High;
                    break;

                case "2":
                    this.Priority = MailPriority.Normal;
                    break;

                case "3":
                    this.Priority = MailPriority.Low;
                    break;
            }
            if (format == "BASIC")
            {
                this.BodyFormat = MailFormat.Text;
            }
            else
            {
                this.BodyFormat = MailFormat.Html;
                this.Body = "<Base Href='" + portalAlias + "'>";
            }
        }

        public void Send()
        {
            try
            {
                string endDelimit;
                ListItem objRecipient;
                string strBody;
                if (this.BodyFormat == MailFormat.Html)
                {
                    endDelimit = "<br>";
                }
                else
                {
                    endDelimit = "\r\n";
                }
                string strConfirmation = "Bulk Email Operation Started: " + DateAndTime.Now.ToString() + endDelimit + endDelimit;
                int intRecipients = 0;
                int intMessages = 0;
                string strDistributionList = "";
                switch (this.SendMethod)
                {
                    case "TO":
                        IEnumerator refObjectHelperL0=null;
                        try
                        {
                            refObjectHelperL0 = this.Recipients.GetEnumerator();
                            while (refObjectHelperL0.MoveNext())
                            {
                                objRecipient = (ListItem) refObjectHelperL0.Current;
                                if (objRecipient.Text != "")
                                {
                                    intRecipients++;
                                    strBody = this.Heading + objRecipient.Value + "," + endDelimit + endDelimit + this.Body;
                                    DotNetNuke.Services.Mail.Mail.SendMail(this.Administrator, objRecipient.Text, "", "", this.Priority, this.Subject, this.BodyFormat, Encoding.UTF8, strBody, this.Attachment, this.SMTPServer, this.SMTPAuthentication, this.SMTPUsername, this.SMTPPassword);
                                    intMessages++;
                                }
                            }
                        }
                        finally
                        {
                            if (refObjectHelperL0 is IDisposable)
                            {
                                (refObjectHelperL0 as IDisposable).Dispose();
                            }
                        }
                        break;

                    case "BCC":
                        IEnumerator refObjectHelperL1=null;
                        try
                        {
                            refObjectHelperL1 = this.Recipients.GetEnumerator();
                            while (refObjectHelperL1.MoveNext())
                            {
                                objRecipient = (ListItem) refObjectHelperL1.Current;
                                if (objRecipient.Text != "")
                                {
                                    intRecipients++;
                                    strDistributionList = strDistributionList + "; " + objRecipient.Text;
                                }
                            }
                        }
                        finally
                        {
                            if (refObjectHelperL1 is IDisposable)
                            {
                                (refObjectHelperL1 as IDisposable).Dispose();
                            }
                        }
                        intMessages = 1;
                        strBody = this.Body;
                        DotNetNuke.Services.Mail.Mail.SendMail(this.Administrator, "", "", strDistributionList, this.Priority, this.Subject, this.BodyFormat, Encoding.UTF8, strBody, this.Attachment, this.SMTPServer, this.SMTPAuthentication, this.SMTPUsername, this.SMTPPassword);
                        break;
                }
                strConfirmation = strConfirmation + "Email Recipients: " + intRecipients.ToString() + endDelimit;
                strConfirmation = (strConfirmation + "Email Messages: " + intMessages.ToString() + endDelimit + endDelimit) + "Bulk Email Operation Completed: " + DateAndTime.Now.ToString() + endDelimit;
                DotNetNuke.Services.Mail.Mail.SendMail(this.Administrator, this.Administrator, "", "", this.Priority, "Bulk Email Confirmation", this.BodyFormat, Encoding.UTF8, strConfirmation, "", this.SMTPServer, this.SMTPAuthentication, this.SMTPUsername, this.SMTPPassword);
            }
            catch (Exception exception1)
            {
                
                Exception exc = exception1;
                Debug.Write(exc.Message);
                
            }
        }
    }
}

